import antlr3
from SqliteLexer import SqliteLexer
from SqliteParser import SqliteParser

"""
splits a dump file into separate CREATE TABLE and Insert statement files
"""

class SplitDump():

    def __init__(self, context):
        self.context = context

    def execute(self):
        print 'splitting dump file'
        self.read_in_file()
        self.parse_contents()


    def read_in_file(self):
        try:
            f = open(self.context.options.source_file, 'r' )
            self.read_data = f.read()
            f.close()
        except IOError as (errno, strerror):
            print "I/O error({0}): {1}".format(errno, strerror)
        except TypeError as strerror:
            print "Type error({0})".format(strerror)
        else:
              f.close()

    def parse_contents(self):
        #to parse a file do antlr3.ANTLRFileStream(path_to_input)
        char_stream = antlr3.ANTLRStringStream(self.read_data)

        lexer = SqliteLexer(char_stream)
        tokens = antlr3.CommonTokenStream(lexer)
        parser = SqliteParser(tokens)
       # begin_transaction = parser.begin_stmt()
       # print begin_transaction.tree.toStringTree()
        stmt = parser.sql_stmt_list()
        print stmt.tree.toStringTree()